package ua.maxicash.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import ua.maxicash.service.AppService;
import ua.maxicash.service.EmployerService;
import ua.maxicash.service.WorkerService;
import ua.maxicash.vo.Worker;
import ua.maxicash.vo.WorkersFreeTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Controller
@RequestMapping("/workers")
public class WorkerController {

    private static final Logger logger = Logger
            .getLogger(WorkerController.class);

    @Autowired
    private EmployerService employerService;

    @Autowired
    private WorkerService workerService;

    @Autowired
    private AppService appService;

    @RequestMapping(value = "/{idWorker}", method = RequestMethod.GET)
    public ModelAndView showWorkersPage(@PathVariable Long idWorker) {

        ModelAndView mav = new ModelAndView("worker");

        Worker worker = workerService.getWorkerById(idWorker);

       if (worker!=null) {
        mav.addObject("worker", worker);

        for (WorkersFreeTime wft : worker.getScheduleList()) {
            switch (wft.getDayOfWeek()) {
                case 0:
                    mav.addObject("monday", wft);
                    break;
                case 1:
                    mav.addObject("tuesday", wft);
                    break;
                case 2:
                    mav.addObject("wednesday", wft);
                    break;
                case 3:
                    mav.addObject("thursday", wft);
                    break;
                case 4:
                    mav.addObject("friday", wft);
                    break;
                case 5:
                    mav.addObject("saturday", wft);
                    break;
                case 6:
                    mav.addObject("sunday", wft);
                    break;
                default:
                    break;

            }
        }
        return mav;
        } else {
            return new ModelAndView("error");
        }

    }


    @RequestMapping(value = "{idWorker}/getImage", method = RequestMethod.GET)
    public void showWorkersImage(@PathVariable Long idWorker,
                                 HttpServletRequest req, HttpServletResponse resp) {

        Worker worker = workerService.getWorkerById(idWorker);

        resp.setContentType("image/jpg");
        try {
            if (worker.getAvatar() != null) {
                resp.getOutputStream().write(worker.getAvatar());
            } else {
                InputStream in = this.getClass().getClassLoader().getResourceAsStream("noAvatar.jpg");
                OutputStream out = resp.getOutputStream();
                int next;
                while ((next = in.read()) != -1)
                    out.write(next);
            }
            resp.getOutputStream().flush();
            resp.getOutputStream().close();
        } catch (IOException e) {
            logger.error(e);
        }

    }

    @RequestMapping(method = RequestMethod.GET)
    public String showListOfWorkers(Model model) {

        model.addAttribute("workers", workerService.listWorker());
        return "listOfWorkers";
    }


}
